package com.zycfc.zsf.boot.job.listener;

import com.dangdang.ddframe.job.lite.api.listener.*;
import com.dangdang.ddframe.job.executor.*;
import com.dangdang.ddframe.job.reg.base.*;
import com.dangdang.ddframe.job.reg.zookeeper.*;
import com.zycfc.zsf.boot.core.common.*;
import com.zycfc.zsf.boot.job.core.*;
import com.zycfc.zsf.boot.util.time.*;
import com.zycfc.zsf.boot.job.common.*;
import com.zycfc.zsf.boot.job.*;
import com.zycfc.zsf.boot.logback.util.*;
import com.zycfc.zsf.boot.util.text.*;
import com.zycfc.zsf.boot.job.api.domain.*;
import com.zycfc.zsf.boot.util.json.*;
import com.dangdang.ddframe.job.lite.internal.sharding.*;
import com.dangdang.ddframe.job.lite.internal.storage.*;
import java.util.*;
import com.zycfc.zsf.boot.util.net.*;
import com.zycfc.zsf.boot.job.service.*;
import java.text.*;
import org.slf4j.*;

public class ZycfcElasticJobListener extends AbstractDistributeOnceElasticJobListener
{
    private static final Logger log;
    
    public ZycfcElasticJobListener(final long startedTimeoutMilliseconds, final long completedTimeoutMilliseconds) {
        super(startedTimeoutMilliseconds, completedTimeoutMilliseconds);
    }
    
    public void doBeforeJobExecutedAtLastStarted(final ShardingContexts shardingContexts) {
        try {
            this.persistStartTm(shardingContexts.getJobName());
        }
        catch (Throwable e) {
            ZycfcElasticJobListener.log.error("ZsfJob: once listener doBeforeJobExecutedAtLastStarted error, jobName[{}] ", (Object)shardingContexts.getJobName(), (Object)e);
        }
    }
    
    private void persistStartTm(final String jobName) {
        final CoordinatorRegistryCenter regCenter = (CoordinatorRegistryCenter)SpringContextHelper.getBean("zookeeperRegistryCenter", (Class)ZookeeperRegistryCenter.class);
        final ZsfJobNodePath zsfJobNodePath = new ZsfJobNodePath(jobName);
        regCenter.persist(zsfJobNodePath.getStartTmNodePath(), String.valueOf(System.currentTimeMillis()));
        final String batchNo = DateUtils.getCurDT() + DateUtils.getCurTM();
        regCenter.persistEphemeral(zsfJobNodePath.getElectBatchNoNodePath(), batchNo);
        ZycfcElasticJobListener.log.info("ZsfJob: doBeforeJobExecutedAtLastStarted-\u751f\u6210\u4e34\u65f6\u6279\u6b21\u53f7\uff0c\u4e34\u65f6\u6279\u6b21\u53f7\u4e3a[{}]", (Object)batchNo);
        final ZsfJobProperties job = JobInfPool.get(jobName);
        if (CallType.CALL_CHAIN_NO.equals(job.getCallType())) {
            ((ZookeeperRegistryCenter)SpringContextHelper.getBean("zookeeperRegistryCenter", (Class)ZookeeperRegistryCenter.class)).persist(zsfJobNodePath.getBatchNoNodePath(), batchNo);
            ZycfcElasticJobListener.log.info("ZsfJob: doBeforeJobExecutedAtLastStarted-\u975e\u4f5c\u4e1a\u94fe\u6ce8\u518c\u6279\u6b21\u53f7\u4fe1\u606f[{}]", (Object)batchNo);
        }
    }
    
    public void doAfterJobExecutedAtLastCompleted(final ShardingContexts shardingContexts) {
        try {
            final ZsfJobProperties job = JobInfPool.get(shardingContexts.getJobName());
            this.execNextJob(job, shardingContexts);
            TraceUtil.endTrace();
        }
        catch (Throwable e) {
            ZycfcElasticJobListener.log.error("ZsfJob: once listener doAfterJobExecutedAtLastCompleted error, jobName[{}] ", (Object)shardingContexts.getJobName(), (Object)e);
        }
    }
    
    private void execNextJob(final ZsfJobProperties job, final ShardingContexts shardingContexts) {
        final String jobName = shardingContexts.getJobName();
        if (StringUtils.isEmpty(jobName)) {
            ZycfcElasticJobListener.log.info("jobName--is-null");
            return;
        }
        final CoordinatorRegistryCenter regCenter = (CoordinatorRegistryCenter)SpringContextHelper.getBean("zookeeperRegistryCenter", (Class)ZookeeperRegistryCenter.class);
        try {
            this.collectionData(regCenter, jobName, shardingContexts.getShardingTotalCount());
        }
        catch (Exception e) {
            ZycfcElasticJobListener.log.error("\u91c7\u96c6\u5168\u5206\u7247\u4efb\u52a1\u6267\u884c\u72b6\u6001\u5931\u8d25[{}]", (Object)jobName, (Object)e);
        }
        final ZsfJobNodePath zsfJobNodePath = new ZsfJobNodePath(jobName);
        final String chainJson = regCenter.get(zsfJobNodePath.getChainNodePath());
        final TJobChainDetail chainDetail = (TJobChainDetail)JsonUtils.jsonToObject(chainJson, (Class)TJobChainDetail.class);
        if (chainDetail == null) {
            ZycfcElasticJobListener.log.info("--chainDetail-is-null");
            return;
        }
        if (StringUtils.isEmpty((List)chainDetail.getSubNode())) {
            ZycfcElasticJobListener.log.info("--getSubNode-is-null");
            return;
        }
        for (final TJobChainDetail nextNode : chainDetail.getSubNode()) {
            final String nextJobName = nextNode.getJobName();
            if (StringUtils.isEmpty(nextJobName)) {
                ZycfcElasticJobListener.log.info("--nextJobName-is-null");
                return;
            }
            final String parentJobNames = nextNode.getParentJobName();
            if (!StringUtils.isEmpty(parentJobNames)) {
                for (final String parentJobName : parentJobNames.split(",")) {
                    final ZsfJobNodePath parentJobNodePath = new ZsfJobNodePath(parentJobName);
                    final String result = regCenter.get(parentJobNodePath.getResultNodePath());
                    if (StringUtils.isEmpty(result)) {
                        ZycfcElasticJobListener.log.info("ZsfJob\u4f5c\u4e1a\u94fe: result is null return");
                        return;
                    }
                    final String sts = result.split("@")[4];
                    if (!"S".equals(sts)) {
                        ZycfcElasticJobListener.log.info("ZsfJob\u4f5c\u4e1a\u94fe: result is not S return");
                        return;
                    }
                    final String logBatchNo = result.split("@")[1];
                    final String preBatchNo = regCenter.get(parentJobNodePath.getPreBatchNoNodePath());
                    if (StringUtils.isEmpty(logBatchNo) && !logBatchNo.equals(preBatchNo)) {
                        return;
                    }
                }
            }
            final ExecutionService executionService = new ExecutionService(regCenter, nextJobName);
            final boolean jobRunning = executionService.hasRunningItems();
            ZycfcElasticJobListener.log.info("ZsfJob\u4f5c\u4e1a\u94fe: \u5f53\u524d\u4efb\u52a1\u8282\u70b9[{}], \u4e0b\u4e00\u4efb\u52a1\u8282\u70b9[{}]\u5206\u7247\u6267\u884c\u72b6\u6001\u4e3a[{}]", new Object[] { parentJobNames, nextJobName, jobRunning });
            if (jobRunning) {
                ZycfcElasticJobListener.log.info("ZsfJob\u4f5c\u4e1a\u94fe: \u4e0b\u4e00\u4e2a\u8282\u70b9[{}]\u6b63\u5728\u6267\u884c, \u6682\u4e0d\u89e6\u53d1!", (Object)nextJobName);
            }
            else {
                this.clearChainHisFailIfNecessary(regCenter, nextJobName);
                final JobNodePath jobNodePath = new JobNodePath(nextJobName);
                for (final String each : regCenter.getChildrenKeys(jobNodePath.getInstancesNodePath())) {
                    regCenter.persist(jobNodePath.getInstanceNodePath(each), "TRIGGER");
                }
                ZycfcElasticJobListener.log.info("ZsfJob\u4f5c\u4e1a\u94fe: \u4e0b\u4e00\u4e2a\u8282\u70b9\u6210\u529f\u89e6\u53d1:{}", (Object)nextJobName);
            }
        }
    }
    
    private void clearElectBatchNoIfNecessary(final CoordinatorRegistryCenter regCenter, final ZsfJobNodePath zsfJobNodePath, final String jobName) {
        final String batchNo = regCenter.get(zsfJobNodePath.getElectBatchNoNodePath());
        if (!StringUtils.isEmpty(batchNo)) {
            regCenter.remove(zsfJobNodePath.getElectBatchNoNodePath());
            ZycfcElasticJobListener.log.info("\u5e38\u89c4\u4efb\u52a1\u5168\u5206\u7247\u6267\u884c\u5b8c\u6210\u540e\u6e05\u7406\u9009\u4e3e\u7684\u6279\u6b21\u53f7\u8282\u70b9\u4fe1\u606f: [{}]", (Object)jobName);
        }
    }
    
    private void clearChainHisFailIfNecessary(final CoordinatorRegistryCenter regCenter, final String nextJobName) {
        final ZsfJobNodePath nextZsfJobNode = new ZsfJobNodePath(nextJobName);
        final String result = regCenter.get(nextZsfJobNode.getResultNodePath());
        if (StringUtils.isEmpty(result)) {
            return;
        }
        final String sts = result.split("@")[4];
        if (!"S".equals(sts)) {
            regCenter.remove(nextZsfJobNode.getResultNodePath());
            ZycfcElasticJobListener.log.info("ZsfJob\u4f5c\u4e1a\u94fe: \u4f5c\u4e1a\u94fe\u89e6\u53d1\u524d\u6e05\u7406\u5b8c\u6210\u4e0b\u4e00\u4e2a\u8282\u70b9\u8fd0\u884c\u5931\u8d25\u7684\u5386\u53f2\u7ed3\u679c[{}]", (Object)nextJobName);
        }
    }
    
    private void collectionData(final CoordinatorRegistryCenter regCenter, final String jobName, final int shardingTotal) {
        final ZsfJobNodePath zsfJobNodePath = new ZsfJobNodePath(jobName);
        final String result = regCenter.get(zsfJobNodePath.getResultNodePath());
        final String bizDate = regCenter.get(zsfJobNodePath.getBizDateNodePath());
        final long startTime = Long.parseLong(regCenter.get(zsfJobNodePath.getStartTmNodePath()));
        final DateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(startTime);
        final String startTm = formatter.format(calendar.getTime());
        final String chainNo = result.split("@")[0];
        final String batchNo = result.split("@")[1];
        final String total = result.split("@")[2];
        final String succ = result.split("@")[3];
        final String sts = result.split("@")[4];
        if (regCenter.isExisted(zsfJobNodePath.getLogFlagNodePath())) {
            final String logFlag = regCenter.get(zsfJobNodePath.getLogFlagNodePath());
            if (logFlag.equals(batchNo)) {
                ZycfcElasticJobListener.log.info("\u5df2\u8bb0\u5f55\u4e0d\u5728\u91cd\u590d\u767b\u8bb0");
                return;
            }
        }
        final String endTm = DateUtils.getCurDT() + DateUtils.getCurTM();
        final ZsfJobDatatRequest request = new ZsfJobDatatRequest(jobName, "1", null, chainNo, batchNo, bizDate, sts, Integer.parseInt(succ), startTm, endTm, System.currentTimeMillis() - startTime, NetUtils.getLocalHost());
        request.setTotal(shardingTotal);
        request.setCurIndex(Integer.parseInt(total));
        final boolean flag = DateCollectionService.doSpanCollenctMessage(request);
        if (flag) {
            regCenter.persist(zsfJobNodePath.getLogFlagNodePath(), batchNo);
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ZycfcElasticJobListener.class);
    }
}
